package com.yanqu.road.entity.activity.sonhai.data;

import com.yanqu.road.entity.DataObject;
import com.yanqu.road.entity.DataStatus;
import com.yanqu.road.entity.FieldTypeAnnotation;
import com.yanqu.road.entity.activity.sonhai.config.SonHaiConfig;
import com.yanqu.road.entity.activity.sonhai.enums.SonHaiRewardFlagEnum;
import com.yanqu.road.entity.activity.sonhai.enums.SonHaiWinFlagEnum;
import com.yanqu.road.utils.date.DateHelper;

import java.util.*;
import java.util.concurrent.ConcurrentHashMap;

@FieldTypeAnnotation(tableName = "t_c_activity_son_hai_egg_data")
public class SonHaiEggData extends DataObject  {

    @FieldTypeAnnotation(desc = "活动ID", select = true, update = true)
    private int activityId;

    @FieldTypeAnnotation(desc = "分组ID", select = true, update = true)
    private int groupId;

    @FieldTypeAnnotation(desc = "蛋唯一ID")
    private int id;

    @FieldTypeAnnotation(desc = "蛋的配置ID")
    private int eggConfigId;

    @FieldTypeAnnotation(desc = "蛋名称")
    private String eggName;

    @FieldTypeAnnotation(desc = "发现者userId")
    private long findUserId;

    @FieldTypeAnnotation(desc = "兽群")
    private SonHaiBeastGroupData beastGroupData;

    @FieldTypeAnnotation(desc = "占领的玩家ID")
    private long userId;

    @FieldTypeAnnotation(desc = "占领的玩家驻守的门客")
    private Map<Integer, SonHaiPatronsData> patronsDataMap = new LinkedHashMap<>();

    @FieldTypeAnnotation(desc = "占领时间")
    private long occupyTime;

    @FieldTypeAnnotation(desc = "孵化成功时间")
    private long completeTime;

    /**
     * 该蛋的挂载地块MAP
     */
    private Map<Long, SonHaiPlotData> attackMeUserPlotMap = new ConcurrentHashMap<>();

    public SonHaiEggData() {}

    public SonHaiEggData(int activityId, int groupId, int id, int eggConfigId, String eggName, SonHaiBeastGroupData beastGroupData, long completeTime) {
        this.activityId = activityId;
        this.groupId = groupId;
        this.id = id;
        this.eggConfigId = eggConfigId;
        this.eggName = eggName;
        this.findUserId = 0;
        this.beastGroupData = beastGroupData;
        this.userId = 0;
        this.occupyTime = 0;
        this.completeTime = completeTime;
        setInsertOption();
    }

    public int getActivityId() {
        return activityId;
    }
    public void setActivityId(int activityId) {
        if (this.activityId != activityId) {
            this.activityId = activityId;
            setOp(DataStatus.Update);
        }
    }

    public int getGroupId() {
        return groupId;
    }

    public void setGroupId(int groupId) {
        if (this.groupId != groupId) {
            this.groupId = groupId;
            setOp(DataStatus.Update);
        }
    }

    public int getId() {
        return id;
    }
    public void setId(int id) {
        if (this.id != id) {
            this.id = id;
            setOp(DataStatus.Update);
        }
    }

    public int getEggConfigId() {
        return eggConfigId;
    }

    public void setEggConfigId(int eggConfigId) {
        this.eggConfigId = eggConfigId;
    }

    public String getEggName() {
        return eggName;
    }

    public void setEggName(String eggName) {
        if (!Objects.equals(this.eggName, eggName)) {
            this.eggName = eggName;
            setOp(DataStatus.Update);
        }
    }

    public long getFindUserId() {
        return findUserId;
    }
    public void setFindUserId(long findUserId) {
        if (this.findUserId != findUserId) {
            this.findUserId = findUserId;
            setOp(DataStatus.Update);
        }
    }

    public SonHaiBeastGroupData getBeastGroupData() {
        return beastGroupData;
    }
    public void setBeastGroupData(SonHaiBeastGroupData beastGroupData) {
        if (null == beastGroupData){
            return;
        }
        if (this.beastGroupData == null || !this.beastGroupData.equals(beastGroupData)) {
            this.beastGroupData = beastGroupData;
            setOp(DataStatus.Update);
        }
    }

    public long getUserId() {
        return userId;
    }
    public void setUserId(long userId) {
        if (this.userId != userId) {
            this.userId = userId;
            setOp(DataStatus.Update);
        }
    }

    public Map<Integer, SonHaiPatronsData> getPatronsDataMap() {
        return patronsDataMap;
    }
    public void setPatronsDataMap(Map<Integer, SonHaiPatronsData> patronsDataMap) {
        if (null == patronsDataMap){
            return;
        }
        if (this.patronsDataMap == null || !this.patronsDataMap.equals(patronsDataMap)) {
            this.patronsDataMap = patronsDataMap;
            setOp(DataStatus.Update);
        }
    }

    public long getOccupyTime() {
        return occupyTime;
    }

    public void setOccupyTime(long occupyTime) {
        if (this.occupyTime != occupyTime) {
            this.occupyTime = occupyTime;
            setOp(DataStatus.Update);
        }
    }

    public long getCompleteTime() {
        return completeTime;
    }
    public void setCompleteTime(long completeTime) {
        if (this.completeTime != completeTime) {
            this.completeTime = completeTime;
            setOp(DataStatus.Update);
        }
    }

    /**
     * 判断玩家是否打过该蛋
     * @param userId
     * @return
     */
    public boolean checkUserBeAttack(long userId) {
        boolean attackedBeast = beastGroupData.checkUserBeAttack(userId);
        boolean attackedUser = false;
        for (SonHaiPatronsData patronsData : this.getPatronsDataMap().values()) {
            if (patronsData.getReBloodInfoMap().containsKey(userId)) {
                attackedUser = true;
            }
        }
        return attackedBeast || attackedUser;
    }

    /**
     * 获取总血量
     * @return
     */
    public long getAllBlood() {
        long allBlood = 0;
        if (this.getUserId() > 0) {
            for (SonHaiPatronsData patronsData : this.patronsDataMap.values()) {
                allBlood += patronsData.getAllBlood();
            }
        } else {
            allBlood = this.getBeastGroupData().getAllBlood();
        }
        return allBlood;
    }

    /**
     * 注入挂载地块
     * @param plotData
     */
    public void addAttackMeUserPlot(SonHaiPlotData plotData) {
        this.attackMeUserPlotMap.put(plotData.getUserId(), plotData);
    }

    /**
     * 移除挂载地块
     * @param userId
     */
    public void removeAttackMeUserPlot(long userId) {
        this.attackMeUserPlotMap.remove(userId);
    }

    /**
     * 清除蛋蛋关联的地块，直接无奖励镇伏这些地块
     * @return
     */
    public List<SonHaiPlotData> clearAttackMeUserPlot() {
        List<SonHaiPlotData> relationalPlotList = new ArrayList<>();//有关联的地块
        for (SonHaiPlotData attackMeUserPlot : this.attackMeUserPlotMap.values()) {
            if (attackMeUserPlot.getWinFlag() == SonHaiWinFlagEnum.WIN_NOT.getType()) {
                //其他指向该蛋的地块，都清除玩家信息，并且自动镇伏，无奖励
                attackMeUserPlot.setWinFlag(SonHaiWinFlagEnum.WIN_YES.getType());
                attackMeUserPlot.setWinTime(System.currentTimeMillis());
                attackMeUserPlot.setGetRewardFlag(SonHaiRewardFlagEnum.GET_YES.getType());
                //影响的地块
                relationalPlotList.add(attackMeUserPlot);
            }
        }
        this.attackMeUserPlotMap.clear();
        return relationalPlotList;
    }

    /**
     * 重置
     */
    public List<SonHaiPlotData> reset(boolean resetCompleteTime) {
        this.setUserId(0);
        this.setFindUserId(0);
        this.setPatronsDataMap(new LinkedHashMap<>());
        this.setOccupyTime(0);
        if (this.beastGroupData != null) {
            this.beastGroupData.reset();
        }
        //清除蛋蛋关联的地块，直接无奖励镇伏这些地块
        List<SonHaiPlotData> relationalPlotList = this.clearAttackMeUserPlot();
        if (resetCompleteTime) {
            //重新设置下蛋的孵化时间，下一天的22点
            long nextCompleteTime = DateHelper.getTimestampNDaysLater(1, 22, 0, 0);
            this.setCompleteTime(nextCompleteTime);
        }
        return relationalPlotList;
    }

    /**
     * 判断是否协助CD
     * @param userId
     * @param cd
     * @return
     */
    public boolean checkHelpCd(long userId, int cd) {
        //判断是否协助cd
        long cdEndTime = this.getHelpCdEndTime(userId, cd);
        if (cdEndTime >= System.currentTimeMillis()) {
            return true;
        }
        return false;
    }

    /**
     * 获取协助cd结束时间
     * @param userId
     * @param cd
     * @return
     */
    public long getHelpCdEndTime(long userId, int cd) {
        long cdEndTime = 0;
        if (!this.getPatronsDataMap().isEmpty()) {
            for (SonHaiPatronsData patronsData : this.getPatronsDataMap().values()) {
                long oneCdEndTime = patronsData.getHelpCdEndTime(userId, cd);
                if (oneCdEndTime > cdEndTime) {
                    cdEndTime = oneCdEndTime;
                }
            }
        } else {
            for (SonHaiBeastData beastData : this.getBeastGroupData().getBeastDataMap().values()) {
                long oneCdEndTime = beastData.getHelpCdEndTime(userId, cd);
                if (oneCdEndTime > cdEndTime) {
                    cdEndTime = oneCdEndTime;
                }
            }
        }
        return cdEndTime;
    }

    /**
     * 获取驻守的门客战力加成
     * @param config
     * @return
     */
    public int getPatronsPowerAddRate(SonHaiConfig config) {
        if (this.occupyTime <= 0) {
            return 0;
        }
        //获取占领了多少分钟
        long diffTime = System.currentTimeMillis() - this.occupyTime;
        long minutes = diffTime / (1000 * 60);
        if (minutes <= 0) {
            return 0;
        }
        int everyMinute = 5;//每X分钟
        int everyMinuteAddRate = 5;//每X分组加成千分比
        int maxAddRate = 250;//最大加成千分比
        //读配置
        List<Integer> paramList = config.getEggPowerAdd();
        if (paramList != null && paramList.size() >= 3) {
            everyMinute = paramList.get(0);
            everyMinuteAddRate = paramList.get(1);
            maxAddRate =  paramList.get(2);
        }
        //最终加成
        return (int) Math.min(maxAddRate, (minutes / everyMinute) * everyMinuteAddRate);
    }
}
